/* Filename: server.c
 * Description: tcp basic版
 * Last modified: humble 2020-06-20 15:54
 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "proto.h"

#define BUFSIZE (1024)
#define IPSTRSIZE (40)

static void usage(void);

static void server_job(int sd)
{
    int len;
    char buf[BUFSIZE] = {0};

    len = sprintf(buf, FMT_STAMP, (long long)time(NULL));
    if(send(sd, buf, len, 0) < 0){
        perror("send()");
        exit(1);
    }
    return ;
}

int main(int argc, char **argv)
{
    int sd, newsd;
    struct sockaddr_in laddr, raddr;
    socklen_t raddr_len;
    char ipstr[IPSTRSIZE] = {0};

    if(argc != 1){
        fprintf(stderr, "argc !=1");
        usage();
        exit(1);
    }

    sd = socket(AF_INET, SOCK_STREAM, 0/* IPPROTO_TCP,IPPROTO_SCTP */);
    if(sd < 0){
        perror("socket()");
        exit(1);
    }

    int val = 1;
    if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0){
        perror("setsockopt()");
        exit(1);
    }

    laddr.sin_family = AF_INET;
    laddr.sin_port = htons(atoi(SERVERPORT));
    inet_pton(AF_INET, "0.0.0.0", &laddr.sin_addr);

    if(bind(sd, (void *)&laddr, sizeof(laddr)) < 0){
        perror("bind()");
        exit(1);
    }

    /* 允许200个连接 */
    if(listen(sd, 200) < 0){
        perror("listen()");
        exit(1);
    }

    raddr_len = sizeof(raddr);

    while(1){
        newsd = accept(sd, (void *)&raddr, &raddr_len);
        if(newsd < 0){
            perror("accept()");
            exit(1);
        }

        inet_ntop(AF_INET, &raddr.sin_addr, ipstr, IPSTRSIZE);
        printf("client:%s:%d\n", ipstr, ntohs(raddr.sin_port));

        server_job(newsd);
        close(newsd);
    }
    close(sd);
    return 0;
}

static void usage(void)
{
#define USAGE "Usage:\n\
  Helloworld\n"
    printf(USAGE);
}
